#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int majorityElement(vector<int>& nums)
    {
        int votes = 0;
        int ret = nums[0];

        for (auto e : nums)
        {
            if (votes == 0) ret = e;
            if (e == ret) votes++;
            else votes--;
        }

        return ret;
    }
};

class Solution {
public:
    int singleNumber(vector<int>& nums)
    {
        int tmp = 0;
        for (auto e : nums) tmp ^= e;
        return tmp;
    }
};
class Solution {
public:
    int minDistance(string word1, string word2)
    {
        int m = word1.size(), n = word2.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

        for (int i = 0; i <= m; i++) dp[i][0] = i;
        for (int j = 0; j <= n; j++) dp[0][j] = j;

        word1 = " " + word1;
        word2 = " " + word2;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (word1[i] == word2[j]) dp[i][j] = dp[i - 1][j - 1];
                else dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
            }
        }

        return dp[m][n];
    }
};
class Solution {
public:
    int longestCommonSubsequence(string s1, string s2)
    {
        int m = s1.size(), n = s2.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        s1 = " " + s1;
        s2 = " " + s2;

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (s1[i] == s2[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
                else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }

        return dp[m][n];
    }
};

